{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'label'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from imblearn.over_sampling import SMOTE\n",
    "from imblearn.under_sampling import RandomUnderSampler\n",
    "pathNew = 'D:/homework/pre/'\n",
    "group1 = pd.read_csv(pathNew+'group1.csv')\n",
    "group2 = pd.read_csv(pathNew+'group2.csv')\n",
    "featureName =group2.columns.tolist()\n",
    "featureName.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped1_y = group1['label']\n",
    "grouped1_x = group1.drop(['label'],axis=1)\n",
    "#使用RandomUnderSampler方法进行欠抽样处理\n",
    "model_RandomUnderSampler = RandomUnderSampler() # 建立RandomUnderSampler模型对象\n",
    "grouped1_x, grouped1_y =model_RandomUnderSampler.fit_sample(grouped1_x,grouped1_y) # 输入数据并作欠抽样处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped1_x = pd.DataFrame(grouped1_x, columns=featureName) # 将数据转换为数据框并命名列名\n",
    "grouped1_y = pd.DataFrame(grouped1_y,columns=['label']) # 将数据转换为数据框并命名列名\n",
    "grouped1 = pd.concat([grouped1_x,grouped1_y],axis=1) # 按列合并数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   iActivePoweiSetPointValue  iAvailabillityToday  iAvailabillityTotal  \\\n",
      "0                        0.0               0.9997               0.9999   \n",
      "\n",
      "   iBlade1TempBattBox_1sec  iBlade1TempInvBox_1sec  iBlade1TempMotor_1sec  \\\n",
      "0                 0.740625                0.726257               0.509599   \n",
      "\n",
      "   iBlade1TempPMCHeatsink_1sec  iBlade1TempPMMHeatsink_1sec  \\\n",
      "0                     0.426987                     0.726098   \n",
      "\n",
      "   iBlade2TempBattBox_1sec  iBlade2TempInvBox_1sec  ...    iUL2_690V  \\\n",
      "0                 0.753846                 0.72067  ...     0.960879   \n",
      "\n",
      "   iUL3_690V  iVaneDiiection  iVibrationY  iVibrationZ  iwindDirection  \\\n",
      "0   0.959228        0.571933     0.457655      0.50313         0.86717   \n",
      "\n",
      "   iWindSpeed  iYPLevel  WT_Runcode  label  \n",
      "0    0.179665       0.0    0.003044      0  \n",
      "\n",
      "[1 rows x 72 columns]\n"
     ]
    }
   ],
   "source": [
    "print(grouped1.head(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   iActivePoweiSetPointValue  iAvailabillityToday  iAvailabillityTotal  \\\n",
      "0                        0.0                  0.0                  0.0   \n",
      "\n",
      "   iBlade1TempBattBox_1sec  iBlade1TempInvBox_1sec  iBlade1TempMotor_1sec  \\\n",
      "0                 0.515625                0.477654               0.244328   \n",
      "\n",
      "   iBlade1TempPMCHeatsink_1sec  iBlade1TempPMMHeatsink_1sec  \\\n",
      "0                     0.268022                     0.488372   \n",
      "\n",
      "   iBlade2TempBattBox_1sec  iBlade2TempInvBox_1sec  ...    iUL2_690V  \\\n",
      "0                     0.56                0.513966  ...     0.954564   \n",
      "\n",
      "   iUL3_690V  iVaneDiiection  iVibrationY  iVibrationZ  iwindDirection  \\\n",
      "0   0.958407        0.465041     0.463626     0.509391        0.871427   \n",
      "\n",
      "   iWindSpeed  iYPLevel  WT_Runcode  label  \n",
      "0    0.120162   0.27027     0.00761      8  \n",
      "\n",
      "[1 rows x 72 columns]\n"
     ]
    }
   ],
   "source": [
    "print(group2.head(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#标签是3的样本只有4个\n",
    "temp = group2[group2['label']==3]\n",
    "group = pd.concat([grouped1,group2,temp],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "group_y = group['label']\n",
    "group_x = group.drop(['label'],axis=1)\n",
    "# 使用SMOTE方法进行过抽样处理\n",
    "model_smote = SMOTE() # 建立SMOTE模型对象\n",
    "group_x, group_y = model_smote.fit_sample(group_x,group_y) # 输入数据并作过抽样处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18792\n"
     ]
    }
   ],
   "source": [
    "m,n = group_x.shape\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped_x = pd.DataFrame(group_x, columns=featureName) # 将数据转换为数据框并命名列名\n",
    "grouped_y = pd.DataFrame(group_y,columns=['label']) # 将数据转换为数据框并命名列名\n",
    "dataSetSampler = pd.concat([grouped_x,grouped_y],axis=1) # 按列合并数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "label\n",
      "0     1566\n",
      "1     1566\n",
      "2     1566\n",
      "3     1566\n",
      "4     1566\n",
      "5     1566\n",
      "6     1566\n",
      "7     1566\n",
      "8     1566\n",
      "9     1566\n",
      "10    1566\n",
      "11    1566\n",
      "Name: label, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "group = dataSetSampler['label'].groupby(dataSetSampler['label'])\n",
    "print(group.count())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSetSampler.to_csv(pathNew+'dataSetSampler.csv',index=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
